/**
 * 2025/01/01 - 2025/12/31
 */
package com.snail.server.iam.account.service;

import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snail.server.iam.account.entity.Account;
import com.snail.server.iam.account.mapper.AccountMapper;
import com.snail.server.iam.error.IamErrorCode;
import com.snail.starter.commons.exception.BizException;
import com.snail.starter.db.enums.EnableOrDisEnableEnum;
import com.snail.starter.utils.tools.CryUtils;
import org.springframework.stereotype.Service;


/**
 * @author zgadmin
 * @date 2025/9/24 17:54
 */
@Service
public class AccountService extends ServiceImpl<AccountMapper, Account> {

    public Account checkAccountByName(String loginName, String password, boolean isMobile) {
        Account account;
        try {
            InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
            account = getOne(Wrappers.lambdaQuery(Account.class)
                    .eq(!isMobile, Account::getLoginName, loginName)
                    .eq(isMobile, Account::getMobile, loginName)
            );
        } finally {
            InterceptorIgnoreHelper.clearIgnoreStrategy();
        }
        if (null == account) {
            throw BizException.of(IamErrorCode.ACCOUNT_NOT_EXISTS, new Object[]{loginName});
        }
        if (!EnableOrDisEnableEnum.ENABLE.equals(account.getStatus())) {
            throw BizException.of(IamErrorCode.ACCOUNT_DISABLE);
        }
        if (!CryUtils.md5(password, loginName).equals(account.getPassword())) {
            throw BizException.of(IamErrorCode.PASSWORD_ERROR);
        }
        return account;
    }

}